package org.bigk.invoices.actions.invoicespositions;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bigk.invoices.actions.AbstractCRUDAction;
import org.bigk.invoices.actions.invoices.InvoicesActionHelper;
import org.bigk.invoices.exceptions.ServiceException;
import org.bigk.invoices.model.Invoice;
import org.bigk.invoices.model.InvoicePosition;
import org.bigk.invoices.model.PaymentKind;
import org.bigk.invoices.model.Tax;
import org.bigk.invoices.services.InvoicesPositionsService;
import org.bigk.invoices.services.InvoicesService;
import org.bigk.invoices.services.PaymentKindsService;
import org.bigk.invoices.services.TaxesService;

public class InvoicesPositionsAction extends AbstractCRUDAction<InvoicePosition> {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(InvoicesPositionsAction.class);
	private static final long serialVersionUID = 3190832719110023997L;
	
	public static final String BACK_INVOICES_INPUT = "backInvoicesInput";
	
	private Long id;
	private Long pId;
	private Invoice invoice;
	
	private List<PaymentKind> paymentKindsList;
	private List<Tax> taxesList;
	
	private InvoicesService invoicesService;
	private InvoicesPositionsService invoicesPositionsService;
	private PaymentKindsService paymentKindsService;
	private TaxesService taxesService;

	@Override
	public void prepare() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("prepare() - start");
		}

		this.invoice = InvoicesActionHelper.retrieveInvoiceFromSession();

		this.paymentKindsList = this.paymentKindsService.listAllItems();
		this.taxesList = this.taxesService.listAllItems();

		if (logger.isDebugEnabled()) {
			logger.debug("prepare() - end");
		}
	}
	
	@Override
	public void prepareCreate() throws ServiceException {
		this.model = this.invoicesPositionsService.prepareNewInvoicePosition();
	}
	
	@Override
	public void prepareEdit() throws ServiceException {
		model = null; // TODO invoicesService.getInvoice(this.getId());
	}

	@Override
	public void prepareSave() throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("prepareSave() - start");
		}

		this.model = new InvoicePosition();

		if (logger.isDebugEnabled()) {
			logger.debug("prepareSave() - end");
		}
	}

	@Override
	public String save() throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("save() - start");
		}

		this.invoicesService.addInvoicePosition(this.invoice, this.model);
		
		if (logger.isDebugEnabled()) {
			logger.debug("save() - end - return value=" + BACK_INVOICES_INPUT);
		}
		return BACK_INVOICES_INPUT;
	}
	
	public Invoice getInvoice() {
		return invoice;
	}

	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPId() {
		return pId;
	}

	public void setPId(Long id) {
		pId = id;
	}

	public List<PaymentKind> getPaymentKindsList() {
		return paymentKindsList;
	}

	public void setPaymentKindsList(List<PaymentKind> paymentKindsList) {
		this.paymentKindsList = paymentKindsList;
	}
	
	public List<Tax> getTaxesList() {
		return taxesList;
	}

	public void setTaxesList(List<Tax> taxesList) {
		this.taxesList = taxesList;
	}

	public InvoicesService getInvoicesService() {
		return invoicesService;
	}

	public void setInvoicesService(InvoicesService invoicesService) {
		this.invoicesService = invoicesService;
	}

	public InvoicesPositionsService getInvoicesPositionsService() {
		return invoicesPositionsService;
	}

	public void setInvoicesPositionsService(
			InvoicesPositionsService invoicesPositionsService) {
		this.invoicesPositionsService = invoicesPositionsService;
	}

	public PaymentKindsService getPaymentKindsService() {
		return paymentKindsService;
	}

	public void setPaymentKindsService(PaymentKindsService paymentKindsService) {
		this.paymentKindsService = paymentKindsService;
	}

	public TaxesService getTaxesService() {
		return taxesService;
	}

	public void setTaxesService(TaxesService taxesService) {
		this.taxesService = taxesService;
	}
}
